package com.company.dcm.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.company.dcm.common.R_Receive;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * @Author: 王泽同
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {

    String filePath = "/usr/share/nginx/html/file";

    @PostMapping("/add")
    public R_Receive addFile(MultipartFile file) throws IOException {
        if (ObjectUtils.isEmpty(file) || file.getSize() <= 0) {
            log.warn("文件为空！");
            return R_Receive.error();
        }

        //原始文件名
        String originalFilename = file.getOriginalFilename();//abc.jpg

        log.info("收到文件："+originalFilename);

        //创建一个目录对象
        File dir = new File(filePath);
        //判断当前目录是否存在
        if (!dir.exists()) {
            //目录不存在，需要创建
            dir.mkdirs();
        }

        try {
            //将临时文件转存到指定位置
            file.transferTo(new File(filePath +"/"+ originalFilename));
        } catch (IOException e) {
            e.printStackTrace();
            return R_Receive.error();
        }
        return R_Receive.success();
    }

    @GetMapping("/download")
    public void findOperatorCharBar2(HttpServletResponse response,
                                     @RequestHeader("fileName") String fileName)throws IOException{
        ServletOutputStream out = null;
        InputStream inputStream = null;

        try {
            File file = new File(filePath +"/"+fileName);
            inputStream = new FileInputStream(file);
            /**
             * 输出文件到浏览器
             */
            int len = 0;
            // 输出 下载的响应头，如果下载的文件是中文名，文件名需要经过url编码
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            response.setHeader("Cache-Control", "no-cache");
            out = response.getOutputStream();
            byte[] buffer = new byte[1024];
            while ((len = inputStream.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                }
            }
        }
    }
}
